package com.css.fxfzypg.modules.report.repository.impl;


import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.report.entity.ReportTemplateEntity;
import com.css.fxfzypg.modules.report.queryParams.ReportTemplateEntityQueryParams;
import com.css.fxfzypg.modules.report.repository.ReportTemplateRepository;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-11-04 15:26
 */
@Repository
public class ReportTemplateRepositoryImpl implements ReportTemplateRepository {



    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Object saveReportTemplate(ReportTemplateEntity reportTemplateEntity) {
        String sql = "insert into sys_report_template (" +
                "id,rt_type,pepole,rt_name,attach_id,rt_time,del_flag,create_user,create_time)" +
                "values" +
                "(:id,:rtType,:pepole,:rtName,:attachId,:rtTime,:delFlag,:createUser,:createTime)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(reportTemplateEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return reportTemplateEntity;
    }

    @Override
    public Map<String, Object> getAllReportTemplate(ReportTemplateEntityQueryParams queryParams) {
        StringBuilder sql = new StringBuilder(" select  u.* from sys_report_template u ");

        StringBuilder orderSql = new StringBuilder(" order by u.rt_time  desc ");

        StringBuilder whereSql = new StringBuilder(" where u.del_flag = '" + YNEnum.N.toString() + "'");

        if (!PlatformObjectUtils.isEmpty(queryParams.getRtType())) {
            whereSql.append(" and u.rt_type = '"+queryParams.getRtType()+"' ");
        }

        if (!PlatformObjectUtils.isEmpty(queryParams.getPepole())) {
            whereSql.append(" and u.pepole  like '%" + queryParams.getPepole() +"%'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getStartTime()) && !PlatformObjectUtils.isEmpty(queryParams.getEndTime())) {
            whereSql.append(" and u.rt_time between '" + queryParams.getStartTime() + "' and '" + queryParams.getEndTime() + "'");
        }

        int firstIndex = (queryParams.getCurPage() - 1) * queryParams.getPageSize();
        int lastIndex = queryParams.getPageSize();
        sql.append(whereSql).append(orderSql).toString();
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ReportTemplateEntity.class)).size();
        List<ReportTemplateEntity> reportTemplateEntities = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(ReportTemplateEntity.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", reportTemplateEntities);
        return map;

    }

    @Override
    public void delReportTemplate(List<String> ids, String userID, Timestamp currentSqlDate) {
        String sql = "update sys_report_template set del_flag = '" + YNEnum.Y + "' , update_time = '" + currentSqlDate + "' ,update_user = '" + userID + "'" + " where id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public List<ReportTemplateEntity> getReportTemplateByType(String rtType) {
        String sql = "select * from sys_report_template where rt_type ='"+rtType+"' and del_flag ='"+YNEnum.N.toString()+"'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(ReportTemplateEntity.class));
    }
}
